package com.example.bjsfpost.sap;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.event.SyncReadListener;
import com.example.bjsfpost.domain.ErrorExcelData;
import com.example.bjsfpost.domain.WuLiaoExcelData;
import com.example.bjsfpost.utils.HttpUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.io.File;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SapAPIInvokeServiceImpl implements SapAPIInvokeService{

    @Value("${sap.bom.url:}")
    private String saBomUrl;
    @Value("${sap.routing.url:}")
    private String sapRoutingUrl;

    @Override
    public SapCommonResponseDO zppMatBomMaintain(SapBomRequestDTO requestDTO) {
        log.info("zppMatBomMaintain SapBomRequestDTO == {}",JSonStr(requestDTO));
        Map<String,String> headers = new HashMap<>();
        headers.put("Accept","text/html,text/plain,application/xhtml+xml,application/xml,application/json");
        headers.put("Content-Type","application/json");
        headers.put("apikey","XdXDTC61ZSiv6X1B10pu5AodEe40UXMw");
        log.info("zppMatBomMaintain url=={},header == {}",saBomUrl,JSonStr(headers));
        String resp = HttpUtil.sendPost(saBomUrl, headers, JSonStr(requestDTO));
        log.info("zppMatBomMaintain response == {}",convertStrEnc(resp));
        resp = convertStrEnc(resp);
        if(StringUtils.hasText(resp)){
            SapCommonResponseDO<SapBomResponseVO> sapCommonResponseDO = JsonStrToClass(resp, SapCommonResponseDO.class);
            SapBomResponseVO sapBomResponseVO = JsonStrToClass(JSonStr(sapCommonResponseDO.getData()), SapBomResponseVO.class);
            sapCommonResponseDO.setData(sapBomResponseVO);
            return sapCommonResponseDO;
        }
        return null;
    }

    @Override
    public SapCommonResponseDO zppRoutingMaintain(SapRoutingRequestDTO requestDTO) {
        log.info("zppRoutingMaintain SapRoutingRequestDTO == {}",JSonStr(requestDTO));
        Map<String,String> headers = new HashMap<>();
        headers.put("Accept","text/html,text/plain,application/xhtml+xml,application/xml,application/json");
        headers.put("Content-Type","application/json");
        headers.put("apikey","XdXDTC61ZSiv6X1B10pu5AodEe40UXMw");
        log.info("zppRoutingMaintain url=={},header == {}",sapRoutingUrl,JSonStr(headers));
        String resp = HttpUtil.sendPost(sapRoutingUrl, headers, JSonStr(requestDTO));
        log.info("zppRoutingMaintain response == {}",convertStrEnc(resp));
        resp = convertStrEnc(resp);
        if(StringUtils.hasText(resp)){
            SapCommonResponseDO<SapRoutingResponseVO> sapCommonResponseDO = JsonStrToClass(resp, SapCommonResponseDO.class);
            SapRoutingResponseVO sapRoutingResponseVO = JsonStrToClass(JSonStr(sapCommonResponseDO.getData()), SapRoutingResponseVO.class);
            sapCommonResponseDO.setData(sapRoutingResponseVO);
            return sapCommonResponseDO;
        }
        return null;
    }



    private String convertStrEnc(String source) {
        if(StringUtils.hasText(source)){
            try {
                String msg = new String(source.getBytes("ISO-8859-1"),"UTF-8");
                return msg;
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }
        return "";
    }
    private String JSonStr(Object req) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.writeValueAsString(req);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    //{"code":500,"msg":"调用sap函数时失败;","data":""}
    private static <T> T JsonStrToClass(String jsonStr,Class<T> clazz) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(jsonStr,clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String resp = "{\"code\":500,\"msg\":\"调用sap函数时失败;\",\"data\":\"\"}";
        SapCommonResponseDO<SapBomResponseVO> sapCommonResponseDO = JsonStrToClass(resp, SapCommonResponseDO.class);
        System.out.println("adsfasdf");
    }
}
